package com.cwg.dict.ui.rcp;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tray;
import org.eclipse.swt.widgets.TrayItem;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;
import org.eclipse.ui.handlers.IHandlerService;
import org.eclipse.ui.plugin.AbstractUIPlugin;

import com.cwg.dict.ui.rcp.views.DictionaryView;

public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {

	private Image		trayImage;
	private TrayItem	trayItem;

	public ApplicationWorkbenchWindowAdvisor(IWorkbenchWindowConfigurer configurer) {
		super(configurer);
	}

	@Override
	public ActionBarAdvisor createActionBarAdvisor(IActionBarConfigurer configurer) {
		return new ApplicationActionBarAdvisor(configurer);
	}

	@Override
	public void dispose() {
		if (trayImage != null) {
			trayImage.dispose();
			trayItem.dispose();
		}
	}

	@Override
	public void postWindowOpen() {
		super.postWindowOpen();
		String[] dictNames = Activator.getDefault().getNetDictNames();
		for (String each : dictNames) {
			String url = Activator.getDefault().getNetDictQueryService().makeQueryUrl(each, "");
			showDictionaryView(each, url);
			Activator.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage().getPerspective();
		}
		final IWorkbenchWindow window = getWindowConfigurer().getWindow();
		trayItem = initTaskItem(window);
		if (trayItem != null) {
			hookPopupMenu(window);
			hookMinimize(window);
		}
	}

	@Override
	public void preWindowOpen() {
		IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
		configurer.setInitialSize(new Point(800, 600));
		configurer.setShowCoolBar(true);
		configurer.setShowStatusLine(false);
	}

	private void hookMinimize(final IWorkbenchWindow window) {
		window.getShell().addShellListener(new ShellAdapter() {
			@Override
			public void shellIconified(ShellEvent e) {
				window.getShell().setVisible(false);
			}
		});
		trayItem.addListener(SWT.DefaultSelection, new Listener() {
			public void handleEvent(Event event) {
				Shell shell = window.getShell();
				if (!shell.isVisible()) {
					shell.setVisible(true);
					window.getShell().setMinimized(false);
				} else {
					shell.setVisible(false);
				}
			}
		});
	}

	private void hookPopupMenu(final IWorkbenchWindow window) {

		Menu menu = new Menu(window.getShell(), SWT.POP_UP);

		// Creates a new menu item that terminates the program
		// when selected
		MenuItem exit = new MenuItem(menu, SWT.NONE);
		exit.setText("Goodbye!");
		exit.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event event) {
				// Lets call our command
				IHandlerService handlerService = (IHandlerService) window.getService(IHandlerService.class);
				try {
					handlerService.executeCommand("exit.command", null);
				} catch (Exception ex) {
					throw new RuntimeException("exit.command not found");
				}
			}
		});
		// We need to make the menu visible
		menu.setVisible(true);

	}

	private TrayItem initTaskItem(IWorkbenchWindow window) {
		final Tray tray = window.getShell().getDisplay().getSystemTray();
		if (tray == null) {
			return null;
		}
		TrayItem trayItem = new TrayItem(tray, SWT.NONE);
		trayImage = AbstractUIPlugin.imageDescriptorFromPlugin("com.cwg.dict.ui.rcp", "icons/toc_closed.gif").createImage();
		trayItem.setImage(trayImage);
		trayItem.setToolTipText("stem dictionary");
		return trayItem;
	}

	private void showDictionaryView(String secondaryId, String url) {
		try {
			IWorkbenchPage page = getWindowConfigurer().getWindow().getActivePage();
			DictionaryView view = (DictionaryView) page.showView(DictionaryView.ID, secondaryId, IWorkbenchPage.VIEW_VISIBLE);
			view.setUrl(url);
			view.setPartName(secondaryId);
		} catch (PartInitException e) {
			e.printStackTrace();
		}
	}

}
